import {defineStore} from "pinia";
import axios from "axios";
import {nanoid} from "nanoid";
//选项式的写法
// export const useLoveTalk = defineStore('loveTalk',{
//     actions:{
//        async getTalk(){
//               try {
//               let {data:{content:title}} =await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
//               let obj = {id:nanoid(),title}
//               this.talkList.unshift(obj)
//             }catch (e) {
//
//             }
//         }
//     },
//     state(){
//         return {
//             talkList :JSON.parse(localStorage.getItem('talk') as string) || []
//         }
//     }
// })

//组合式的写法
import {reactive} from "vue";
export const useLoveTalk = defineStore('loveTalk',()=>{
    let talkList = reactive( JSON.parse(localStorage.getItem('talk') as string) || [])
    async function getTalk(){
     try {
              let {data:{content:title}} =await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
              let obj = {id:nanoid(),title}
             talkList.unshift(obj)
            }catch (e) {

            }
        }
        return {talkList,getTalk}
})